#include "frienditem.h"
#include <QPainter>
#include <QPixmap>
#include <QStyleOption>
#include "ui_frienditem.h"
FriendItem::FriendItem(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FriendItem)
{
    ui->setupUi(this);
}

FriendItem::~FriendItem()
{
    delete ui;
}

void FriendItem::setFriendInfo(std::shared_ptr<FriendInfo> friendInfo)
{
    _friendInfo = friendInfo;
    QPixmap pix(friendInfo->icon);
    pix = pix.scaled(ui->friend_head->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->friend_head->setPixmap(pix);
    if (friendInfo->remark.isEmpty()) {
        ui->friend_name->setText(friendInfo->name);
    } else {
        ui->friend_name->setText(friendInfo->remark);
    }
}

void FriendItem::paintEvent(QPaintEvent *event)
{
    // 创建一个QStyleOption对象
    QStyleOption opt;
    opt.initFrom(this);
    // 创建一个QPainter对象，用于绘制
    QPainter p(this);
    // 使用样式表绘制控件
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    // 调用父类的paintEvent方法，处理其他绘制事件
    QWidget::paintEvent(event);
}

void FriendItem::mousePressEvent(QMouseEvent *event)
{
    emit sig_friendClicked(_friendInfo);
}
